/**
 * @param {number[]} satisfaction
 * @return {number}
 */

var swap = function(array, x, y) {
  const temp = array[x]
  array[x] = array[y]
  array[y] = temp
  return array;
}

// 运行时间: 92 ms,,,,, 内存消耗: 37.9 MB
var maxSatisfaction = function(satisfaction) {
  const length = satisfaction.length
  for (let i = 0; i < length - 1; i++) {
    let min = i
    
    for (let j = i + 1; j < length; j++ ) {
      min = satisfaction[min] > satisfaction[j] ? j : min
    }

    if (i != min) {
      swap(satisfaction, i, min)
    }
    // console.log('min=', min, satisfaction)
  }
  satisfaction.sort((a, b) => {
    return a - b
  })
  // 如果最后的(也是最大的数) 小于0, 这个数组中所有数都不要
  if (satisfaction[length -1] < 0) return 0
  
  // console.log('有正数')
  // console.log('satisfaction=', satisfaction)

  let sum = 0;
  // let startIndex = 1;
  for (let i = 0; i < length; i++) {
    let index = 1;
    let tempSum = 0;
    // console.log("------>")
    for (let j = i; j < length; j++) {
      const step = satisfaction[j] * index
      // console.log('step=', step, index)
      tempSum += step;
      index ++
    }

    if (sum < tempSum) {
      sum = tempSum;
      // startIndex = i;
    }
  }
  // console.log('startIndex =', startIndex)
  return sum;
};

// const list = [-1,-8,0,5,-9]
// let list = [4,3,2]
// const list = [-1,-4,-5]
const list = [-2,5,-1,0,3,-3]
console.log(list)
console.log("--")
console.log(maxSatisfaction(list))